<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/1/12
 * Time: 19:55
 */
namespace Home\Controller;
use Admin\Model\AssayClassModel;
use Admin\Model\HealthRecordModel;
use Admin\Model\LinkModel;
use Base\Controller\BaseHomeController;
use Library\Record;

class  HealthRecordController extends BaseHomeController  {
    protected $model;
    public  function __construct()
    {
        parent::__construct();
        $this->model= new HealthRecordModel();
    }

    public  function info(){
        $where['paitent_id']=I('id');
        $where['status_id']=I('status_id');
        $where['status']=1;
        $linkMmodel = new LinkModel();
        $data = $linkMmodel->relation(true)->where($where)->find();
        $this->assign('info',$data);
        $this->show();
     }
    /**
     * @return bool
     * 修改患者患者状态类型
     */
    public function editstatus(){
        $where['patient_id']=I('patient_id');
        $where['doctor_id']=session('ys_user');
        $linkMmodel = new LinkModel();
        switch (I('type')){
            case 0 :
                $map['status_id']=-1;
                break;
            case 1:
                $map['status_id']=2;
                break;
            case 2 :
                $map['status_id']=4;
                break;
            default;
                return false;
                break;
        }
        $data=$linkMmodel->where($where)->save($map);
        if($data){
           $this->success('设置成功',U('Index/index'));
        }else{
            $this->error('设置失败');
        }

    }

    public function assay(){
        $where['status']=1;
        $where['assay_class_id']=0;
        $assayModel = new AssayClassModel();
        $data= $assayModel->where($where)->field('id,title')->select();
        $this->assign('assay',$data);
        $this->show();
    }
    public function resulted(){
          $where['id']=I('id');
          $where['status']=1;
          $data= $this->model->where($where)->getField('content');
          $data=json_decode($data,true);
          $this->assign('result',$data);
          $this->show();
    }
    public function EditResulted(){
        $this->show();
    }



    public function  assaydata($assay_class_id = ''){
        $where['status']=1;
        $where['assay_class_id']=$assay_class_id;
        $assayModel = new AssayClassModel();
        $data= $assayModel->where($where)->select();
        $this->ajaxreturn($data);
    }
    /**
     * 添加化验单
     */
    public  function  addRecord(){
        $data['title']=$_POST['title'];
        $data['patient_id']=$_POST['patient_id'];
        $data['record_class_id']=2;
        $items = $_POST['assay'];
        $content = array();
        foreach ($items as $k=>$v){
            $record = new Record($k+1,$v,'','检查记录');
            $item = $record->toArray();
            $content[$k] = $item;
        }
         $data['content'] =  json_encode($content);
         $Info  =  $this->model->create($data);
         $recordInfo  =  $this->model->add($Info);
        if($recordInfo){
            $data['info']='添加成功';
            $data['status']=1;
         $this->ajaxReturn($data);
        }else{
           $this->error('添加失败');
        }
    }


}